﻿using Kanban.DALContainer;
using Kanban.IBLL;
using Kanban.IDal;
using Kanban.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kanban.BLL
{
    public class ProjectProgressService : BaseService<ProjectProgress>, IProjectProgressService
    {
        private readonly IProjectProgressDal ProjectProgressDal = Container.Resolve<IProjectProgressDal>();

        public override void SetCurrentDal()
        {
            Dal = ProjectProgressDal;
        }

        public async Task<Tuple<IList<object>, int>> GetModelsByPageAsync(string search, int pageSize, int pageIndex)
        {
            return await ProjectProgressDal.GetModelsByPageAsync(search, pageSize, pageIndex);
        }

        public async Task<bool> Save(ProjectProgress model)
        {
            var query = await GetModelAsync(model.Id);
            if (query == null)
            {
                model.StageOverview = model.StageOverview ?? "";
                model.ProcessProblem = model.ProcessProblem ?? "";
                model.ManagerName = model.ManagerName ?? "";
                model.MEIds = model.MEIds ?? "";
                model.MEs = model.MEs ?? "";
                model.EEIds = model.EEIds ?? "";
                model.EEs = model.EEs ?? "";
                model.SEIds = model.SEIds ?? "";
                model.SEs = model.SEs ?? "";
                model.PRIds = model.PRIds ?? "";
                model.PRs = model.PRs ?? "";
                return await AddAsync(model) > 0;
            }
            else
            {
                query.Name = model.Name;
                query.StartTime = model.StartTime;
                query.PlannedShippingTime = model.PlannedShippingTime;
                query.PlannedCompleteTime = model.PlannedCompleteTime;
                query.Status = model.Status;
                query.Stage = model.Stage;
                query.StageOverview = model.StageOverview;
                query.ProcessProblem = model.ProcessProblem;
                query.ManagerName = model.ManagerName;
                query.MEs = model.MEs;
                query.EEs = model.EEs;
                query.SEs = model.SEs;
                query.PRs = model.PRs;
                return await UpdateAsync(query) > 0;
            }
        }
    }
}
